#include "drv_alarm_logic.h"
#include "drv_calib.h"
#include "drv_recoder.h"
#include "main.h"
#include "smoke.h"
#include "drv_ntc_temp.h"
#define SE_NUM	(SMK+1)
enum
{
	H2=0,CO,T1,T2,SMK
};

uint16_t fad_h2(void)
{
	uint16_t res=0;
	res=ad_get_channel(H2_AD_CH);
	return res;
}
uint16_t fad_co(void)
{
	uint16_t res=0;
	res=ad_get_channel(CO_AD_CH);
	return res;
}
uint16_t fad_t1(void)
{
	uint16_t res=0;
	res=ad_get_channel(T1_AD_CH);
	return res;
}
uint16_t fad_t2(void)
{
	uint16_t res=0;
	res=ad_get_channel(T2_AD_CH);
	return res;
}
uint16_t fad_smk(void)
{
	uint16_t res=0;
	res=ad_get_channel(SMK_AD_CH);
	return res;
}
/************************///dir
int16_t fcov_h2(int16_t a)
{return 0;}
int16_t fcov_co(int16_t a)
{return 0;}
int16_t fcov_t1(int16_t a)
{
	int16_t res=0;
	res=(int16_t)Get_Tempturex(a);
	return res;
}
int16_t fcov_t2(int16_t a)
{
	int16_t res=0;
	res=(int16_t)Get_Tempturex(a);
	return res;
}
int16_t fcov_smk(int16_t a)
{return 0;}
/************************///covBycalib
//int16_t fcovcali_h2(_sensor_t *_t)
//{
//	_fcovCalib_val(_t);
//}
//int16_t fcovcali_co(_sensor_t *_t)
//{
//	_fcovCalib_val(_t);
//}
//int16_t fcovcali_t1(_sensor_t *_t)
//{
//	_fcovCalib_val(_t);
//}
//int16_t fcovcali_t2(_sensor_t *_t)
//{
//	_fcovCalib_val(_t);
//}
//int16_t fcovcali_smk(_sensor_t *_t)
//{
//	_fcovCalib_val(_t);
//}



void _sensor_para_cfg(void)
{
	status_t[H2].use=1;
	status_t[CO].use=1;
	status_t[T1].use=1;
	status_t[T2].use=1;
	status_t[SMK].use=1;
	
	lmt_t[H2].lmt_h=800;
	lmt_t[H2].lmt_l=600;	
	lmt_t[CO].lmt_h=800;
	lmt_t[CO].lmt_l=600;
	lmt_t[T1].lmt_h=800;
	lmt_t[T1].lmt_l=600;
	lmt_t[T2].lmt_h=800;
	lmt_t[T2].lmt_l=600;
	lmt_t[SMK].lmt_h=800;
	lmt_t[SMK].lmt_l=600;
	
	calib_t[H2].needcalib_fla=1;
	calib_t[CO].needcalib_fla=1;
	calib_t[T1].needcalib_fla=0;
	calib_t[T2].needcalib_fla=0;
	calib_t[SMK].needcalib_fla=1;
	
	calib_t[H2].calib_num=2;
	calib_t[CO].calib_num=2;
	calib_t[T1].calib_num=0;
	calib_t[T1].calib_num=0;
	calib_t[SMK].calib_num=2;
	
	calib_t[H2].calib_arr[0]=(point_t){0,0};
	calib_t[H2].calib_arr[1]=(point_t){100,100};
	calib_t[CO].calib_arr[0]=(point_t){0,0};
	calib_t[CO].calib_arr[1]=(point_t){100,100};
	calib_t[SMK].calib_arr[0]=(point_t){0,0};
	calib_t[SMK].calib_arr[1]=(point_t){100,100};
}



void sensor_init(void)
{
	_sensor_para_cfg();
	
	_sensor_rate_cfg(&sensor_t[H2],10000,_rate_cal);
	_sensor_rate_cfg(&sensor_t[CO],10000,_rate_cal);
	_sensor_rate_cfg(&sensor_t[T1],10000,_rate_cal);
	_sensor_rate_cfg(&sensor_t[T2],10000,_rate_cal);
	_sensor_rate_cfg(&sensor_t[SMK],10000,_rate_cal);
	
	_sensor_fad_cfg(&sensor_t[H2],fad_h2);
	_sensor_fad_cfg(&sensor_t[CO],fad_co);
	_sensor_fad_cfg(&sensor_t[T1],fad_t1);
	_sensor_fad_cfg(&sensor_t[T2],fad_t2);
	_sensor_fad_cfg(&sensor_t[SMK],fad_smk);
	
	_sensor_fcovVAL(&sensor_t[H2],_fcovCalib_val,NULL);
	_sensor_fcovVAL(&sensor_t[CO],_fcovCalib_val,NULL);
	_sensor_fcovVAL(&sensor_t[T1],NULL,fcov_t1);
	_sensor_fcovVAL(&sensor_t[T2],NULL,fcov_t2);
	_sensor_fcovVAL(&sensor_t[SMK],_fcovCalib_val,fcov_smk);
	
	_sensor_init(&sensor_t[H2],(uint8_t *)"h2",&status_t[H2],&lmt_t[H2],&calib_t[H2]);
	_sensor_init(&sensor_t[CO],(uint8_t *)"co",&status_t[CO],&lmt_t[CO],&calib_t[CO]);
	_sensor_init(&sensor_t[T1],(uint8_t *)"t1",&status_t[T1],&lmt_t[T1],&calib_t[T1]);
	_sensor_init(&sensor_t[T2],(uint8_t *)"t2",&status_t[T2],&lmt_t[T2],&calib_t[T2]);
	_sensor_init(&sensor_t[SMK],(uint8_t *)"smk",&status_t[SMK],&lmt_t[SMK],&calib_t[SMK]);
	
	
	
	p_h2=&sensor_t[H2];
	p_co=&sensor_t[CO];
	p_t1=&sensor_t[T1];
	p_t2=&sensor_t[T2];
	p_smk=&sensor_t[SMK];

}
void sensor_loop(void)
{
	_sendor_run(&sensor_t[H2]);
	_sensor_lmt_det(&sensor_t[H2]);
	_rate_cal(&sensor_t[H2]);
	
	_sendor_run(&sensor_t[CO]);
	_sensor_lmt_det(&sensor_t[CO]);
	_rate_cal(&sensor_t[CO]);
	
	_sendor_run(&sensor_t[T1]);
	_sensor_lmt_det(&sensor_t[T1]);
	_rate_cal(&sensor_t[T1]);
	
	_sendor_run(&sensor_t[T2]);
	_sensor_lmt_det(&sensor_t[T2]);
	_rate_cal(&sensor_t[T2]);

	_sendor_run(&sensor_t[SMK]);
	_sensor_lmt_det(&sensor_t[SMK]);
	_rate_cal(&sensor_t[SMK]);

}
volatile uint8_t	resx[8]={0};
volatile uint8_t  resx_ad[SE_NUM][3]={0};
volatile uint8_t 	sensor_idx=0;
volatile uint8_t 	calib_idx=0;
uint8_t _transmit_fla=0xff;
void drv_sensor_RWreg(uint8_t *dat)
{
	uint16_t boo=0;
	
	uint8_t	sensor_kind=0xff;
	uint8_t	fuc=0xff;
	uint8_t calib_index=0;
	fuc=dat[1];
	sensor_kind=dat[2];
	calib_index=dat[3];
	if((dat[2]!=0xaa)&&(dat[3]==0x55))//restore factory cfg
	{
		if(sensor_kind>=(SE_NUM))return;
	}
	switch(fuc)
	{
		case 0X01://add calib
		{
				sensor_t[sensor_kind].val_x_y.x=sensor_t[sensor_kind].ad;//change to get input num -->diy
				sensor_t[sensor_kind].val_x_y.y=dat[3]<<8|dat[4];
				add_point(sensor_t[sensor_kind].val_x_y,&sensor_t[sensor_kind].calib_buf.calib_num,sensor_t[sensor_kind].calib_buf.calib_arr);
				point_sort(sensor_t[sensor_kind].calib_buf.calib_arr, sensor_t[sensor_kind].calib_buf.calib_num);
				drv_recoder_write_buf(SENSOR_FLASH_ADD(sensor_kind),&sensor_t[sensor_kind]);
				_transmit_fla=RESP(0X01);
				resx[2]=sensor_t[sensor_kind].val_x_y.x>>8;
				resx[3]=sensor_t[sensor_kind].val_x_y.x;
				resx[4]=sensor_t[sensor_kind].val_x_y.y>>8;
				resx[5]=sensor_t[sensor_kind].val_x_y.y;
		}break;
		case 0x02://write lmt_l
		{
				boo=dat[3]<<8|dat[4];
				sensor_t[sensor_kind].lmt_buf.lmt_l=boo;
				drv_recoder_write_buf(SENSOR_FLASH_ADD(sensor_kind),&sensor_t[sensor_kind]);
				_transmit_fla=RESP(0X02);
				resx[2]=sensor_t[sensor_kind].lmt_buf.lmt_l>>8;
				resx[3]=sensor_t[sensor_kind].lmt_buf.lmt_l;
		}break;
		case 0x03://write lmt_h
		{
				boo=dat[3]<<8|dat[4];
				sensor_t[sensor_kind].lmt_buf.lmt_h=boo;
				drv_recoder_write_buf(SENSOR_FLASH_ADD(sensor_kind),&sensor_t[sensor_kind]);
				_transmit_fla=RESP(0X03);
				resx[2]=sensor_t[sensor_kind].lmt_buf.lmt_h>>8;
				resx[3]=sensor_t[sensor_kind].lmt_buf.lmt_h;
		}break;
		case 0x04://del calib
		{
				del_point(dat[3],&sensor_t[sensor_kind].calib_buf.calib_num,sensor_t[sensor_kind].calib_buf.calib_arr);
				drv_recoder_write_buf(SENSOR_FLASH_ADD(sensor_kind),&sensor_t[sensor_kind]);
				_transmit_fla=RESP(0X04);
				
		}break;
		case 0x05://read calib num
		{
				drv_recoder_read_buf(SENSOR_FLASH_ADD(sensor_kind),&sensor_t[sensor_kind]);
				resx[2]=sensor_kind;
				resx[3]=sensor_t[sensor_kind].calib_buf.calib_num;
				_transmit_fla=RESP(0X05);
		}break;
		case 0x06://read lmt_l
		{
				drv_recoder_read_buf(SENSOR_FLASH_ADD(sensor_kind),&sensor_t[sensor_kind]);
				resx[2]=sensor_kind;
				resx[3]=sensor_t[sensor_kind].lmt_buf.lmt_l>>8;
				resx[4]=sensor_t[sensor_kind].lmt_buf.lmt_l;
				_transmit_fla=RESP(0x06);
		}break;
		case 0x07://read lmt_h
		{
				drv_recoder_read_buf(SENSOR_FLASH_ADD(sensor_kind),&sensor_t[sensor_kind]);
				resx[2]=sensor_kind;
				resx[3]=sensor_t[sensor_kind].lmt_buf.lmt_h>>8;
				resx[4]=sensor_t[sensor_kind].lmt_buf.lmt_h;
				_transmit_fla=RESP(0x07);
		}break;
		case 0x08://read indexth calib 
		{
				drv_recoder_read_buf(SENSOR_FLASH_ADD(sensor_kind),&sensor_t[sensor_kind]);
				resx[2]=sensor_kind;
				resx[3]=sensor_t[sensor_kind].calib_buf.calib_arr[calib_index].x>>8;
				resx[4]=sensor_t[sensor_kind].calib_buf.calib_arr[calib_index].x;
				resx[5]=sensor_t[sensor_kind].calib_buf.calib_arr[calib_index].y>>8;
				resx[6]=sensor_t[sensor_kind].calib_buf.calib_arr[calib_index].y;
				_transmit_fla=RESP(0x08);
		}break;
		case 0x09://write addr id
		{
			if(dat[3]!=0)
			{
				device_t.addr=dat[3];
				_transmit_fla=RESP(0x09);
				resx[2]=device_t.addr;
				drv_recoder_write_device_buf(DEVICE_FLASH_ADD,&device_t);
			}
		}break;
		case 0x0a://read addr id
		{
				drv_recoder_read_device_buf(DEVICE_FLASH_ADD,&device_t);
				resx[2]=device_t.addr;
				_transmit_fla=RESP(0x0a);
		}break;
		case 0x0b://write baud 
		{
			if(dat[3]==20||dat[3]==50||dat[3]==100)
			{
				device_t.baud=dat[3];
				
			}
			else
			{
				device_t.baud=BAUD_DEFAULT;
			}
			drv_recoder_write_device_buf(DEVICE_FLASH_ADD,&device_t);
		
			resx[2]=device_t.baud;
			_transmit_fla=RESP(0x0b);
		}break;
		case 0x0c://read baud 
		{
				drv_recoder_read_device_buf(DEVICE_FLASH_ADD,&device_t);
				resx[2]=device_t.baud;
				_transmit_fla=RESP(0x0c);
				
		}break;
		case 0x0d://regist
		{
				resx[2]=device_t.addr;
				_transmit_fla=RESP(0x0d);
		}break;
		case 0x0e://query val
		{
				resx[2]=(uint8_t)sensor_t[CO].val>>8;
				resx[3]=(uint8_t)sensor_t[CO].val;
				resx[4]=(uint8_t)sensor_t[H2].val>>8;
				resx[5]=(uint8_t)sensor_t[H2].val;
				resx[6]=((uint8_t)sensor_t[T1].val+(uint8_t)sensor_t[T2].val)/2;
				resx[7]=(uint8_t)sensor_t[SMK].val;
				_transmit_fla=RESP(0x0e);
		}break;
		case 0x0f://query ad
		{		
				resx_ad[CO][0]=CO;
				resx_ad[CO][1]=(uint8_t)sensor_t[CO].ad>>8;
				resx_ad[CO][2]=(uint8_t)sensor_t[CO].ad;
			
				resx_ad[H2][0]=H2;
				resx_ad[H2][1]=(uint8_t)sensor_t[H2].ad>>8;
				resx_ad[H2][2]=(uint8_t)sensor_t[H2].ad;
			
				resx_ad[T1][0]=T1;
				resx_ad[T1][1]=(uint8_t)sensor_t[T1].ad>>8;
				resx_ad[T1][2]=(uint8_t)sensor_t[T1].ad;
			
				resx_ad[T2][0]=T2;
				resx_ad[T2][1]=(uint8_t)sensor_t[T2].ad>>8;
				resx_ad[T2][2]=(uint8_t)sensor_t[T2].ad;
			
				resx_ad[SMK][0]=SMK;
				resx_ad[SMK][1]=(uint8_t)sensor_t[SMK].ad>>8;
				resx_ad[SMK][2]=(uint8_t)sensor_t[SMK].ad;
				_transmit_fla=RESP(0x0f);
		}break;
		case 0x10://query status
		{
				resx[2]=sensor_t[CO].sta.lv|sensor_t[CO].sta.use<<4;
				resx[3]=sensor_t[H2].sta.lv|sensor_t[H2].sta.use<<4;
				resx[4]=sensor_t[T1].sta.lv|sensor_t[T1].sta.use<<4;
				resx[5]=sensor_t[T2].sta.lv|sensor_t[T2].sta.use<<4;
				
				resx[6]=sensor_t[SMK].sta.lv|sensor_t[SMK].sta.use<<4;
				resx[7]=device_t.status;
				
				_transmit_fla=RESP(0x10);
		}break;
		case 0x11://set use
		{
				
				sensor_t[sensor_kind].sta.use=dat[3];
				drv_recoder_write_buf(SENSOR_FLASH_ADD(sensor_kind),&sensor_t[sensor_kind]);
				resx[2]=sensor_kind;
				resx[3]=sensor_t[sensor_kind].sta.use;
				_transmit_fla=RESP(0x11);
		}break;
		case 0x12://read use
		{
				
				drv_recoder_read_buf(SENSOR_FLASH_ADD(sensor_kind),&sensor_t[sensor_kind]);
				resx[3]=sensor_t[sensor_kind].sta.use;
				_transmit_fla=RESP(0x12);
		}break;
		case 0x13:
		{
				if((dat[2]==0xaa)&&(dat[3])==0x55)
				{
					drv_have_recoder_init_buf();
					_transmit_fla=RESP(0x13);
				}
				break;
		}
		default:
			break;
		
	
	}
	memset(dat,0xff,8);//clear
}

#include "gd32f30x_can.h"
uint8_t tx_code[]={0x81,0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90,0x91,0x92,0x93};
can_trasnmit_message_struct CAN_tx_t,CAN_tx1_t;
uint8_t tx_code_num=0;
uint8_t test=0;
uint8_t en_tx_fla=0;
uint8_t _cnt_=0;
void drv_transmit(void)
{
//	memcpy(CAN_tx_t.tx_data,(uint8_t *)resx,8);
//	can_message_transmit(CAN0,&CAN_tx_t);
	test++;
	
	if(en_tx_fla==0)return;
	en_tx_fla=0;
//	if(Juge(_transmit_fla)!=MSK)return;
	tx_code_num=sizeof(tx_code)/sizeof(tx_code[0]);
	resx[0]=device_t.addr;
	resx[1]=_transmit_fla;
	memcpy(CAN_tx_t.tx_data,(uint8_t *)resx,8);
	CAN_tx_t.tx_efid=DEVICE_ADD_S|device_t.addr;
	CAN_tx_t.tx_ff=CAN_FF_EXTENDED;
	
		for(uint8_t i=0;i<tx_code_num;i++)
		{
			if(_transmit_fla==tx_code[i])
			{
					if(_transmit_fla==0x8f)
					{
						for(_cnt_=0;_cnt_<SEN_NUM;_cnt_++)
						{
							CAN_tx_t.tx_dlen=8;
							CAN_tx_t.tx_data[2]=resx_ad[_cnt_][0];
							CAN_tx_t.tx_data[3]=resx_ad[_cnt_][1];
							CAN_tx_t.tx_data[4]=resx_ad[_cnt_][2];
							can_message_transmit(CAN0,&CAN_tx_t);
							delay_ms(100);
						}
						//break;
					}
					else
					{
							CAN_tx_t.tx_dlen=8;
							can_message_transmit(CAN0,&CAN_tx_t);
							//break;
					}
			}
		}
	
//	for(uint8_t i=0;i<tx_code_num;i++)
//	{
//		if(_transmit_fla==tx_code[i])//support code function
//		{
//			CAN_tx_t.tx_dlen=8;
//			can_message_transmit(CAN0,&CAN_tx_t);
//			break;
//		}
//	}
		if(_transmit_fla==0x93)
	{
		NVIC_SystemReset();
	}
	_transmit_fla=0xff;
	memset((uint8_t *)resx,0,8);

	
}
void drv_get_recoder_init(void)
{
	if(drv_have_recoder_read_buf())//Not written
	{
		drv_recoder_write_buf(SENSOR_FLASH_ADD(CO),&sensor_t[CO]);
		drv_recoder_write_buf(SENSOR_FLASH_ADD(H2),&sensor_t[H2]);
		drv_recoder_write_buf(SENSOR_FLASH_ADD(T1),&sensor_t[T1]);
		drv_recoder_write_buf(SENSOR_FLASH_ADD(T2),&sensor_t[T2]);
		drv_recoder_write_buf(SENSOR_FLASH_ADD(SMK),&sensor_t[SMK]);
		drv_recoder_write_device_buf(DEVICE_FLASH_ADD,&device_t);
		drv_have_recoder_write_buf();
		NVIC_SystemReset() ;
	}
	else
	{
		drv_recoder_read_buf(SENSOR_FLASH_ADD(CO),&sensor_t[CO]);
		drv_recoder_read_buf(SENSOR_FLASH_ADD(H2),&sensor_t[H2]);
		drv_recoder_read_buf(SENSOR_FLASH_ADD(T1),&sensor_t[T1]);
		drv_recoder_read_buf(SENSOR_FLASH_ADD(T2),&sensor_t[T2]);
		drv_recoder_read_buf(SENSOR_FLASH_ADD(SMK),&sensor_t[SMK]);
		drv_recoder_read_device_buf(DEVICE_FLASH_ADD,&device_t);
	}
}
extern void delay_1ms(uint32_t count);
void drv_device_status_det(void)
{
	uint8_t boo=0;
	uint8_t foo[8]={0};
	uint8_t reflash_fla=0;
	if(device_t.old_status!=device_t.status)
	{
		device_t.old_status=device_t.status;
		reflash_fla=1;
	}
	if(reflash_fla==1)
	{
			
			CAN_tx1_t.tx_efid=DEVICE_ADD_S|device_t.addr;
			CAN_tx1_t.tx_ff=CAN_FF_EXTENDED;
			foo[0]=device_t.addr;
			foo[1]=0xff;
			foo[2]=sensor_t[CO].sta.lv|sensor_t[CO].sta.use<<4;
			foo[3]=sensor_t[H2].sta.lv|sensor_t[H2].sta.use<<4;
			foo[4]=sensor_t[T1].sta.lv|sensor_t[T1].sta.use<<4;
			foo[5]=sensor_t[T2].sta.lv|sensor_t[T2].sta.use<<4;
			
			foo[6]=sensor_t[SMK].sta.lv|sensor_t[SMK].sta.use<<4;
			foo[7]=device_t.status;
		CAN_tx1_t.tx_dlen=8;
			memcpy(CAN_tx1_t.tx_data,(uint8_t *)foo,8);
		for(boo=0;boo<3;boo++)
		{
			delay_1ms(1000);
			can_message_transmit(CAN0,&CAN_tx1_t);
		}
		memset(foo,0,8);
		reflash_fla=0;	
	}
}
void drv_device_loop(void)
{
	drv_device_status_det();//other logic
	

}